package com.tianhui.consignortwofei.other.auth.recognition;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.fgs.common.utils.LogUtil;
import com.fgs.common.widget.CustomProgressDialog;
import com.google.gson.reflect.TypeToken;
import com.tianhui.consignortwofei.mvp.model.enty.RecognitionInfo;
import com.tianhui.consignortwofei.mvp.model.enty.SelectPhotoInfo;
import com.tianhui.consignortwofei.mvp.model.enty.authLicense.VehicleLicenseData;
import com.tianhui.consignortwofei.mvp.model.enty.ocrResult.roadTransport.RoadTransportResult;
import com.tianhui.consignortwofei.other.auth.inte.RecognitionCallback;
import com.tianhui.consignortwofei.other.auth.ocr.huawei.HWOcrClientAKSK;
import com.xuexiang.xutil.net.JsonUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @author fgs
 * @date 2021/5/13
 * @description
 **/
public class VehicleRoadRecognition implements IRecognition<RoadTransportResult> {


    @Override
    public void recognition(Context context, SelectPhotoInfo selectPhotoInfo, RecognitionCallback<RoadTransportResult> callback) {
        if (context != null && callback != null) {
            Bitmap bitmap = BitmapFactory.decodeFile(selectPhotoInfo.filePath);
            if (bitmap != null) {
                akskOcrService(context, bitmap, callback);
            } else {
                callback.recognitionFailed();
            }

        }

    }

    private void akskOcrService(Context context, Bitmap bitmap, RecognitionCallback<RoadTransportResult> callback) {
        String AK;
        String SK;
        AK = "1RT86FMYE8UCF08LZ0DE";// AK from authentication
        SK = "QfCN8OidNzrWmnyrL1GEa1uHcgCChAd6iDqTCgqw";// SK from authentication

        String region = "cn-north-4";
        // initialize HWOcrClientAKSK from ak,sk and endpoint information
        HWOcrClientAKSK hwOcrClientAKSK = new HWOcrClientAKSK(context, AK, SK, region);
        // ocr service
        String uri = "/v1.0/ocr/transportation-license";
        // Set params except image
        JSONObject params = new JSONObject();
        CustomProgressDialog dialog = new CustomProgressDialog(context);
        dialog.show();
        hwOcrClientAKSK.requestOcrAkskService(uri, bitmap, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                    callback.recognitionFailed();
                    LogUtil.print(e.toString());
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                    String result = response.body().string();
                    LogUtil.print(result);
                    RoadTransportResult resultInfo = JsonUtil.fromJson(result, new TypeToken<RoadTransportResult>() {
                    }.getType());
                    if (resultInfo == null) {
                        LogUtil.print("扫描失败");
                        return;
                    }

                    RecognitionInfo recognitionInfo = new RecognitionInfo<VehicleLicenseData>();
                    recognitionInfo.info = resultInfo;

                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            callback.recognitionSuccess(recognitionInfo);
                        }
                    });


                    if (!TextUtils.isEmpty(resultInfo.error_code)) {
                        LogUtil.print(resultInfo.error_code + "\n" + resultInfo.error_msg);
                    }
                }
            }
        });
    }

}
